﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAL
{
    public class AddGoodsDAL
    {

        #region[AddGoods_SelectALL]
        public List<DTO.AddGoodsDTO> AddGoods_SelectALL()
        {
            List<DTO.AddGoodsDTO> adgall = new List<DTO.AddGoodsDTO>();
            
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("SelectAllAddGoods", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                DTO.AddGoodsDTO adg = new DTO.AddGoodsDTO();
                adg.AddID = dr.GetInt32(0);
                adg.Date = dr.GetDateTime(1);
                adg.TotalAddCost = dr.GetInt32(2);
                adg.SupplierName = dr.GetString(3);
                adg.Address = dr.GetString(4);
                adg.Phone = dr.GetString(5);
                adgall.Add(adg);
            }
            dr.Close();
            con.Close();

            return adgall;
        }
        #endregion

        #region[AddGoods_SelectByDate]
        public List<DTO.AddGoodsDTO> AddGoods_SelectByDate(DateTime date)
        {
            List<AddGoodsDTO> list = new List<AddGoodsDTO>();
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("SelectAddGoodsByDate", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@date", date);
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                DTO.AddGoodsDTO adg = new DTO.AddGoodsDTO();
                adg.AddID = dr.GetInt32(0);
                adg.Date = dr.GetDateTime(1);
                adg.TotalAddCost = dr.GetInt32(2);
                adg.SupplierName = dr.GetString(3);
                adg.Address = dr.GetString(4);
                adg.Phone = dr.GetString(5);
                list.Add(adg);
            }
            dr.Close();
            con.Close();
            return list;
        }
        #endregion

        #region[AddGoods_SelectByID]
        public DTO.AddGoodsDTO AddGoods_SelectByID(int id)
        {
            DTO.AddGoodsDTO adg = new DTO.AddGoodsDTO();
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("SelectAddGoodsByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@addid", id);
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                adg.AddID = dr.GetInt32(0);
                adg.Date = dr.GetDateTime(1);
                adg.TotalAddCost = dr.GetInt32(2);
                adg.SupplierName = dr.GetString(3);
                adg.Address = dr.GetString(4);
                adg.Phone = dr.GetString(5);
            }
            dr.Close();
            con.Close();
            return adg;
        }
        #endregion

        #region [AddGoods_Insert]
        public bool AddGoods_Insert(DTO.AddGoodsDTO adg)
        {
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("AddAddGoods", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new MySqlParameter("@date", adg.Date));
            cmd.Parameters.Add(new MySqlParameter("@totaladdcost", adg.TotalAddCost));
            cmd.Parameters.Add(new MySqlParameter("@suppliername", adg.SupplierName));
            cmd.Parameters.Add(new MySqlParameter("@address", adg.Address));
            cmd.Parameters.Add(new MySqlParameter("@phone", adg.Phone));

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            return true;
        }
        #endregion

    }
}
